class_name Editor extends Node2D

@onready var ui = $"UI"
var prefab_point = preload("res://地图编辑/地图块-编辑模式/PointEditor.tscn")

var map_points = Dictionary()
var selected_point:PointEditor

func _init():
	ObjectManager.editor = self

func _ready():
	var x:float = -14.5		# 横坐标 -15 到 15
	var y:int = -15			# 纵坐标 -15 到 15
	while y < 16:
		while x < 16:
			var node:PointEditor = prefab_point.instantiate()
			$"战场".add_child(node)
			node.init(x,y)
			x+=1
		y+=1
		if abs(y%2) == 1:
			x = -14.5
		else:
			x = -15

func _process(_delta):
	if KeyBoardInputManager.is_click(KeyBoardInputManager.KEYCODE_WU):
		pointTerrainChange(-1)
	if KeyBoardInputManager.is_click(KeyBoardInputManager.KEYCODE_KONG):
		pointTerrainChange(0)
	if KeyBoardInputManager.is_click(KeyBoardInputManager.KEYCODE_SHAN):
		pointTerrainChange(1)
	if KeyBoardInputManager.is_click(KeyBoardInputManager.KEYCODE_SHUI):
		pointTerrainChange(2)
	if KeyBoardInputManager.is_click(KeyBoardInputManager.KEYCODE_LIN):
		pointTerrainChange(3)	
	if KeyBoardInputManager.is_click(KeyBoardInputManager.KEYCODE_SHA):
		pointTerrainChange(4)
	if KeyBoardInputManager.is_click(KeyBoardInputManager.KEYCODE_JING):
		pointTerrainChange(5)
		
func pointTerrainChange(terr:int):
	selected_point.changeTerrain(terr)


func _on_导出_pressed():
	var arr = []
	for point in map_points.values():
		if (point as PointEditor).real_terrain != -1:
			var obj = {"x":point.x,"y":point.y,"terrain":point.real_terrain}
			arr.append(obj)
	var file = FileAccess.open("res://地图编辑/导出地图/map.json", FileAccess.WRITE)
	file.store_string(JSON.stringify(arr))
	print("导出完成")
